import * as mine from "../../api/mine";
import { getNewToken } from "../../utils/http-server";
var appInst =  getApp();
import validate from "../../utils/validate";

Page({
    data: {
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        userInfo:{},
        session_key:"",
        userName:"",
        mobile:""
    },
    handle_auto_cancel(){
        wx.navigateBack({
            delta: 1
        });
    },
    getUserProfile(e) {

        let that = this;
        let userInfo = {}

        wx.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {

                userInfo = res.userInfo;
                that.setData({
                    userInfo:userInfo
                });
                // getNewToken()
                //     .then((res)=>{
                //         that.setData({
                //             userInfo:userInfo,
                //             session_key:res.data.data.session_key
                //         });
                //     })

                //let userInfo = res.userInfo;
            }
        })
    },
    get_input_value(e){
        let { id } = e.currentTarget;
        let value = e.detail.value;
        this.setData({
            [`${id}`]:value
        });
    },
    submit(){

        if(!this.data.userName){
            wx.showToast({
                title: '请输入员工姓名',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }
        if(!this.data.mobile){
            wx.showToast({
                title: '请输入手机号',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }
        if( !validate.mobile.test( this.data.mobile ) ){
            wx.showToast({
                title: '手机号格式错误',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }


        mine.login({
            "userName": this.data.userName,
            "mobile": this.data.mobile,
            ...this.data.userInfo
        })
        .then((res)=>{
            wx.navigateBack({
                delta: 1
            });
        })
    },
})
